// pages/orderList/index.js
import {
  ApiOrderDelayList,
  ApiOrderList,
  ApiOrderPay,
  ApiCancelPay
} from '../../api/index'
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl,
    commonUpload,
    status: "1",
    statusFormatter: [{
      label: '未处理',
      value: '0'
    }, {
      label: '已拒绝',
      value: '1'
    }, {
      label: '已通过',
      value: '2'
    }, ],
    active: '步骤一',
    steps: [{
        text: '已接单',
      },
      {
        text: '已出发',
      },
      {
        text: '已上门',
      },
      {
        text: '已完成',
      },
    ],
    dataList: [],
    active: '全部',
    List: []
  },

  onShow() {
    this.getOrderList()
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    /*    this.getOrderList('全部') */
  },
  /*   onShow() {
      this.getOrderList('全部')
    }, */

  //申请延期
  reviewExtension(e) {
    wx.navigateTo({
      url: `/pages/reviewExtension/index?data=${JSON.stringify(e.currentTarget.dataset.v)}`,
    })
  },
  //拿到订单列表数据

  getOrderList() {
    ApiOrderDelayList().then(res => {
      if (res.code == 200) {
        /*   this.data.List = [] */
        res.data.forEach(item => {
          //添加字段
          this.checkStatus(item)
          /*  this.filterValue(val, item) */
          /*   if (item.price_detail && item.price_detail.length > 0) {
              let val = JSON.parse(item.price_detail);
              let data = [];
              for (const key in val) {
                data.push({
                  name: key,
                  value: val[key]
                });
              }
              item.price_detailCopy = data
            } */
        })



        this.setData({
          dataList: res.data
        })
      }
    })
  },
  //改变状态
  checkStatus(item) {
    this.data.statusFormatter.forEach(statusItem => {
      if (item.status == statusItem.value) {
        item.statusLabel = statusItem.label
      }
    })
  },

  onClick(e) {
    console.log(e)
    /*    wx.navigateTo({
         url: `/components/detailPage/index?id=${e.currentTarget.dataset.v.order.id}`,
       }) */
    let valOrder = e.currentTarget.dataset.v.order
    if (valOrder.price_detail && valOrder.price_detail.length > 0) {
      let val = JSON.parse(valOrder.price_detail);
      let data = [];
      for (const key in val) {
        data.push({
          name: key,
          value: val[key]
        });
      }
      valOrder.price_detailCopy = data
    }
    wx.navigateTo({
      url: `/pages/orderListDetail/index?data=${JSON.stringify(valOrder)}`,
    })
  },
  // 过滤
  filterValue(val, item) {
    if (val == '全部') {
      this.data.List.push(item)
    } else {
      if (item.statusLabel == val) {
        this.data.List.push(item)
      }
    }

  },
  //切换页面
  onChangeTab(e) {
    console.log(e)
    this.setData({
      active: e.detail.name
    })
    this.getOrderList(e.detail.name)
  },
  //跳转到订单详情
  bindClickDetail(e) {
    console.log(e)
    wx.navigateTo({
      url: `/pages/orderListDetail/index?data=${JSON.stringify(e.currentTarget.dataset.v)}`,
    })
  },
  //继续付款
  continuePay(e) {
    ApiOrderPay(e.currentTarget.dataset.v.id).then(res => {
      if (res.code == '200') {
        let that = this
        wx.requestPayment({
          ...res.data,
          success(res) {
      /*       app.clearStatistics() */
            wx.showModal({
              title: '支付成功',
              showCancel: false,
              success(res) {
                this.setData({
                  active: '全部'
                })
                that.getOrderList('全部')
                /*   if (that.data.luckyDrawTime) {
                    wx.redirectTo({
                      url: '/pages/activity/index?router=/activity/paymentDraw',
                    })
                  } else {
                    pathBack("pages/user/payOrInvoice/pay/index", 1)
                  } */
              }
            })
          },
          fail(res) {
            console.log('1111', res)
            wx.showModal({
              title: '支付失败',
              showCancel: false
            })
          }


        })
      }
    })
  },
  //取消订单
  cancelPay(e) {
    let that = this
    wx.showModal({
      title: "是否确认取消订单？",
      success(res) {
        if (res.confirm) {
          ApiCancelPay(e.currentTarget.dataset.v.id).then(res => {
            if (res.code == '200') {
              wx.showToast({
                title: "取消成功",
              })
              that.setData({
                active: '已取消'
              })
              that.getOrderList('已取消')
            }
          })
        }
      },
      fail(err) {
        console.log(err)
      }
    })


  },
  //立即评价
  evaluation(e) {
    wx.navigateTo({
      url: `/pages/evaluation/index?id=${JSON.stringify(e.currentTarget.dataset.v)}`,
    })
  },
  //查看评价
  SearchEvaluation(e) {
    wx.navigateTo({
      url: `/pages/searchEvaluation/index?id=${e.currentTarget.dataset.v.id}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})